package gvjclient.actions;

import gvjclient.GVJclient;
import gvjclient.ui.FixedSizePlainDocument;
import gvjclient.ui.GVJclientUI;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class SendSMSAction extends AbstractAction
{

	private String destinationNumber;
	private GVJclient client;
	private JTextField sourceText;
	
	public SendSMSAction(GVJclient client, String destinationNumber)
	{
		super("SMS");
		this.destinationNumber = destinationNumber;
		this.client = client;
	}
	
	public SendSMSAction(GVJclient client)
	{
		super("SMS");
		this.client = client;
	}

	public SendSMSAction()
	{
		super("SMS");
		ImageIcon imageIcon = new ImageIcon(getClass().getResource(File.separator + "images" + File.separator + "sms.png"));
		this.putValue(SHORT_DESCRIPTION, "Send SMS");
		this.putValue(SMALL_ICON, imageIcon);
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		JLabel msgLabel;
		JTextArea textArea;
		JTextField destinationText;
		JLabel sourceLabel;
		JLabel destinationLabel;
		JLabel accountLabel;
		int result;
		JPanel panel;
		GVJclient[] clients;
		int i;
		String text;
		ActionListener accountActionListener;
		KeyListener keyListener;
		JComboBox accountCombo = null;
		int textAreaRows = 8;
		Dimension preferredSize = new Dimension(175,25);

		panel = new JPanel();
		panel.setLayout(new FlowLayout());
		msgLabel = new JLabel("Do not enter more than 160 characters.");
		sourceLabel = new JLabel("Source");
		destinationLabel = new JLabel("Destination");
		accountLabel = new JLabel("Account");
		sourceText = new JTextField();
		destinationText = new JTextField();
		accountCombo = new JComboBox();
		
		sourceText.setEnabled(false);
		accountActionListener = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Object[] clients;
				JComboBox source = (JComboBox)e.getSource();
				clients = source.getSelectedObjects();
				if(clients.length>0)
				{
					client = (GVJclient)clients[0];
					populateSourceText();
				}
			}
		};
		
		keyListener = new DestinationNumberKeyListener();

		if (this.client != null)
		{
			accountCombo.addItem(this.client);
			accountCombo.setEnabled(false);
			this.sourceText.setText(this.client.getPrimaryDID());
		}
		else //this.client == null
		{
			clients = GVJclientUI.get_instance().getLoggedInClients();
			accountCombo.removeAllItems();
			System.out.println("null");
			if(clients.length>0)
			{
				for(i=0;i<clients.length;i++)
				{
					accountCombo.addItem(clients[i]);
				}
				accountCombo.addActionListener(accountActionListener);
				accountCombo.setSelectedItem(clients[0]);
			}
		}
		if ( this.destinationNumber != null)
		{
			destinationText.setText(destinationNumber);
			destinationText.setEnabled(false);
		}
		else
		{
			destinationText.addKeyListener(keyListener);
		}

		textArea = new JTextArea();
		textArea.setRows(textAreaRows);
		textArea.setDocument(new FixedSizePlainDocument(160));
		textArea.setLineWrap(true);
		panel.add(accountLabel);
		panel.add(accountCombo);
		panel.add(sourceLabel);
		panel.add(sourceText);
		panel.add(destinationLabel);
		panel.add(destinationText);
		panel.add(textArea);
		panel.add(msgLabel);
		panel.setPreferredSize(new Dimension(400,300));
		accountLabel.setPreferredSize(preferredSize);
		accountCombo.setPreferredSize(preferredSize);
		sourceLabel.setPreferredSize(preferredSize);
		sourceText.setPreferredSize(preferredSize);
		destinationLabel.setPreferredSize(preferredSize);
		destinationText.setPreferredSize(preferredSize);
		textArea.setPreferredSize(new Dimension(350,120));
		msgLabel.setPreferredSize(new Dimension(350,25));

		textArea.addHierarchyListener(new HierarchyListener() {
	        public void hierarchyChanged(HierarchyEvent e) {
	            final Component c = e.getComponent();
	            if (c.isShowing() && (e.getChangeFlags() &
	                HierarchyEvent.SHOWING_CHANGED) != 0) {
	                Window toplevel = SwingUtilities.getWindowAncestor(c);
	                toplevel.addWindowFocusListener(new WindowAdapter() {
	                    public void windowGainedFocus(WindowEvent e) {
	                        c.requestFocus();
	                    }
	                });
	            }
	        }
	    });
		result = JOptionPane.showConfirmDialog(null,
						panel,
						"Send SMS",JOptionPane.OK_CANCEL_OPTION,JOptionPane.PLAIN_MESSAGE);
		if ( result == JOptionPane.OK_OPTION)
		{
			 
			try {
					text = textArea.getText();
					this.destinationNumber = destinationText.getText();
					client.sendSMS(destinationNumber,text);
				} catch (IOException ex) {
					System.out.println("Error sending SMS");
				}
		}
		this.client = null;
		this.destinationNumber = null;
		this.sourceText = null;
	}
	
	private void populateSourceText()
	{
		this.sourceText.setText(this.client.getPrimaryDID());
	}
}